package com.taotao.manage.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * 内容管理
 * @author DuBo
 *
 */
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.commen.EasyUiResult;
import com.taotao.manage.pojo.Content;
import com.taotao.manage.service.ContentService;
/**
 * 内容管理
 * @author DuBo
 *
 */
@Controller
@RequestMapping("content")
public class ContentController {

	
	@Autowired
	private ContentService  contentService;
	
	
	/**
	 * 根据分类id查找内容
	 * @param categoryId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<EasyUiResult> queryContent(@RequestParam("categoryId")Long categoryId,@RequestParam(value="page",defaultValue="1") Integer pageNum,@RequestParam(value="rows",defaultValue="10") Integer pageSize ){
		try {
			Content record = new Content();
			record.setCategoryId(categoryId);
			PageInfo<Content> pageInfo = this.contentService.queryPageListByWhere(pageNum, pageSize, record);
			EasyUiResult result = new EasyUiResult(pageInfo.getTotal(), pageInfo.getList());
			return ResponseEntity.status(HttpStatus.OK).body(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * 添加内容
	 * @param content
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> addContent(Content content){
		Boolean flag = this.contentService.save(content);
		if (flag) {
			return ResponseEntity.status(HttpStatus.CREATED).build();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	
	
}
